﻿using Saisie_des_prélèvements.Models;
using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Saisie_des_prélèvements.ViewModels
{
    public class ClientTakenViewModel : ViewModelBase
    {
        #region Properties

        private int client_taken_id = 0;
        public int ClientTakenId
        {
            get
            { return client_taken_id; }

            set
            {
                if (client_taken_id == value)
                { return; }

                client_taken_id = value;
                RaisePropertyChanged("ClientTakenId");
            }
        }

        private string client_taken_code = string.Empty;
        public string ClientTakenCode
        {
            get
            { return client_taken_code; }

            set
            {
                if (client_taken_code == value)
                { return; }

                client_taken_code = value;
                RaisePropertyChanged("ClientTakenCode");
            }
        }

        private string client_taken_label = string.Empty;
        public string ClientTakenLabel
        {
            get
            { return client_taken_label; }

            set
            {
                if (client_taken_label == value)
                { return; }

                client_taken_label = value;
                RaisePropertyChanged("ClientTakenLabel");
            }
        }

        private int? zone_id;
        public int? ZoneId
        {
            get
            { return zone_id; }

            set
            {
                if (zone_id == value)
                { return; }

                zone_id = value;
                RaisePropertyChanged("ZoneID");
            }
        }

        private int? cont_id;
        public int? ContractId
        {
            get
            { return cont_id; }

            set
            {
                if (cont_id == value)
                { return; }

                cont_id = value;
                RaisePropertyChanged("ContractId");
            }
        }

        private int? user_create = 0;
        public int? UserCreate
        {
            get
            { return user_create; }

            set
            {
                if (user_create == value)
                { return; }

                user_create = value;
                RaisePropertyChanged("UserCreate");
            }
        }

        private DateTime? date_create = System.DateTime.Today;
        public DateTime? DateCreate
        {
            get
            { return date_create; }

            set
            {
                if (date_create == value)
                { return; }

                date_create = value;
                RaisePropertyChanged("DateCreate");
            }
        }

        public string Adr1 { get; set; }
        public string Adr2 { get; set; }
        public string Adr3 { get; set; }
        public string CodePostal { get; set; }
        public string Ville { get; set; }
        public string Phone1 { get; set; }
        public string Phone2 { get; set; }
        public string BGColor { get; set; }
        public string Status { get; set; }
        public string Lock { get; set; }
        #endregion "Properties"

        private ObservableCollection<ClientTakenViewModel> clients;
        public ObservableCollection<ClientTakenViewModel> Clients
        {
            get
            {
                return clients;
            }

            set
            {
                clients = value;
                RaisePropertyChanged("Clients");
            }
        }

        public List<ClientTakenViewModel> GetClientsByZone(int ZoneId)
        {

            var db = new SQLite.SQLiteConnection(App.DBPath);
            List<ClientTakenViewModel> re = db.Query<ClientTakenViewModel>(
                string.Format("SELECT c.client_taken_id ClientTakenId, c.client_taken_code ClientTakenCode, n.TX_NOM ClientTakenLabel, n.TX_ADR1 Adr1, n.TX_ADR2 Adr2, n.TX_ADR3 Adr3, n.TX_CP CodePostal, n.TX_VILLE Ville, n.TX_TEL Phone1, n.TX_MOBILE Phone2, c.cont_id ContractId,  '#ffffff' BGColor, GROUP_CONCAT(DISTINCT k.status) Status, CASE IFNULL(signature, 0) WHEN 0 THEN 'ms-appx:///Assets/Very-Basic-Unlock-icon.png' ELSE 'ms-appx:///Assets/Very-Basic-Lock-icon.png' END Lock FROM k_client_taken c JOIN NTIERS_X n ON c.client_taken_code= n.TX_CODE AND c.zone_id={0} JOIN k_taken k ON c.client_taken_id=k.client_taken_id GROUP BY c.client_taken_id ORDER BY c.client_taken_code", ZoneId));

            return re;
        }


    }
}
